<?php

namespace app\merchant\model\store;

use app\common\model\store\Access as AccessModel;
use app\store\model\store\RoleAccess;
use org\Redis;
/**
 * 商家用户权限模型
 * Class Access
 * @package app\store\model\store
 */
class Access extends AccessModel
{


    /**
     * 获取权限列表 jstree格式
     * @param int $role_id 当前角色id
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAccessTree($role_id = null)
    {

        if(is_null($role_id)){
            $where = ['parent_id'=>0,'is_show'=>0];
            $menu = $this->getAccessMenu($where,$accessIds = []);
        }else{
            $accessIds = is_null($role_id) ? [] : RoleAccess::getAccessIds($role_id);
            $where = ['parent_id'=>0,'is_show'=>0];
            $menu = $this->getAccessMenu($where,$accessIds);
        }

        return $menu;
    }



    /**
     * 根据权限获取菜单
     * @param $access_id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function getAccessMenu($where,$accessIds = [])
    {

        //一级菜单
        $menuinfo = db("store_access")
            ->where($where)
            ->field(['access_id,name,parent_id'])
            ->order('sort asc')
            ->select()->toArray();

        foreach($menuinfo as $k=>&$v){
            $menuinfo[$k]['checked'] = (in_array($v['access_id'], $accessIds));
            //左侧二级菜单
            $menuinfo[$k]['child'] = db("store_access")
                ->where("is_show = 0 and parent_id=".$v['access_id'])
                ->field(['access_id,name,parent_id'])
                ->order('sort asc')
                ->select()->toArray();

            foreach($menuinfo[$k]['child'] as $kk=>&$vv){
                $menuinfo[$k]['child'][$kk]['checked'] = (in_array($vv['access_id'], $accessIds));
                //左侧三级菜单
                $menuinfo[$k]['child'][$kk]['childchild'] = db("store_access")
                    ->field(['access_id,name,parent_id'])
                    ->where("is_show = 0 and parent_id=".$vv['access_id'])
                    ->order('sort asc')
                    ->select()->toArray();


                foreach($menuinfo[$k]['child'][$kk]['childchild'] as $kkk=>&$vvv){
                    $menuinfo[$k]['child'][$kk]['childchild'][$kkk]['checked'] = (in_array($vvv['access_id'], $accessIds));
                }
            }

        }

        return  $menuinfo;

    }



    /**
     * 获取权限列表 jstree格式
     * @param int $role_id 当前角色id
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getJsTree($role_id = null)
    {

        if(is_null($role_id)){
            $where = ['parent_id'=>0,'type'=>0,'is_show'=>0];
            $menu = $this->getMenu($where,$accessIds = []);
        }else{
            $accessIds = is_null($role_id) ? [] : RoleAccess::getAccessIds($role_id);
            $where = ['parent_id'=>0,'type'=>0,'access_id'=>['in',$accessIds],'is_show'=>0];
            $menu = $this->getMenu($where,$accessIds);
        }

        return $menu;
    }

    /**
     * 根据权限获取菜单
     * @param $access_id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function getMenu($where,$accessIds = [])
    {
        $store_user_id = self::$store_user_id;

        $redis = new Redis();

        $menuinfo = $redis->hGet('merchantMenu',$store_user_id);

//        if($menuinfo){
//            return json_decode($menuinfo,true);
//        }
        //左侧一级菜单
        $menuinfo = db("store_access")
            ->where($where)
            ->field(['access_id,name,url,parent_id,icon,type'])
            ->order('sort asc')
            ->select()->toArray();

        $where1 = [];
        if(!empty($accessIds)){

            $where1 = ['is_show'=>0,'access_id'=>['in',$accessIds]];
        }
        foreach($menuinfo as $k=>$v){
            //左侧二级菜单
            $menuinfo[$k]['child']= db("store_access")
                ->where("parent_id=".$v['access_id']." and type=0")
                ->where($where1)
                ->field(['access_id,name,url,parent_id,icon,type'])
                ->order('sort asc')
                ->select()->toArray();

            foreach($menuinfo[$k]['child'] as $m=>$v){
                //左侧三级菜单
                $menuinfo[$k]['child'][$m]['childchild'] = db("store_access")
                    ->field(['access_id,name,url,parent_id,icon,type'])
                    ->where("parent_id=".$v['access_id']." and type=0 and is_show=0")
                    ->where($where1)
                    ->order('sort asc')
                    ->select()->toArray();

            }
        }

        $redis->hSet('merchantMenu',$store_user_id,json_encode($menuinfo),86400 * 1);

        return  $menuinfo;

    }




}